package com.example.forum2.Controller;

import com.example.forum2.Service.IArticleService;
import com.example.forum2.Service.IMessageService;
import com.example.forum2.Service.IUserService;
import com.example.forum2.comment.AppResult;
import com.example.forum2.comment.ResultCode;
import com.example.forum2.config.AppConfig;
import com.example.forum2.model.Message;
import com.example.forum2.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private IMessageService messageService;
    @Autowired
    private IUserService userService;
    @RequestMapping("/send")
    public AppResult send(HttpServletRequest request,Long receiveUserId,String content){
        HttpSession session = request.getSession();
        User user= (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        if(user.getId()==receiveUserId){
            return AppResult.failed("不能给自己发送站内信");
        }
        User receiverUser = userService.selectById(receiveUserId);
        if(receiverUser==null||receiverUser.getDeleteState()==1){
            return AppResult.failed("接受者状态异常");
        }
        Message message = new Message();
        message.setPostUserId(user.getId());
        message.setReceiveUserId(receiveUserId);
        message.setContent(content);
        messageService.create(message);
        return AppResult.success("发送完成");
    }
    @RequestMapping("/getUnreadCount")
    public AppResult<Integer> getUnreadCount(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
      Integer result=  messageService.selectUnreadCount(user.getId());
        return AppResult.success(result);
    }
    @RequestMapping("/getAll")
    public AppResult<List<Message>> getAll(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        List<Message> messages = messageService.selectByReceiveUserId(user.getId());
        return AppResult.success(messages);
    }
    @RequestMapping("/markRead")
    public AppResult markRead(Long id,HttpServletRequest request){
        Message message = messageService.selectById(id);
        if(message==null||message.getDeleteState()==1){
            return AppResult.failed(ResultCode.FAILED);
        }
        HttpSession session = request.getSession();
        User user=(User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getId()!=message.getReceiveUserId()){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        messageService.updateStateById(id,(byte) 1);
        return AppResult.success();
    }
    @RequestMapping("/reply")
    public AppResult reply(HttpServletRequest request,Long repliedId,String content){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        Message existMessage = messageService.selectById(repliedId);
        if(existMessage==null||existMessage.getDeleteState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        if(user.getId()==existMessage.getPostUserId()){
            return AppResult.failed("不能给自己发信息");
        }
        Message message = new Message();
        message.setPostUserId(user.getId());
        message.setReceiveUserId(existMessage.getReceiveUserId());
        message.setContent(content);
        messageService.reply(repliedId,message);
        return AppResult.success();
    }
}
